// xc/components/addr/location/label.js
var locationhelper = require('../../../sys/locationhelper') 
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    location:Object,
    distance:null,
    maxdistance:Number,
    themcolor: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    locationaddr: null,
    locationaddrtxt:"定位中...",
  },
  ready: function () { 
    var app = getApp() 
   
    if(app.globalData.member)
    {
      if(!app.globalData.userset.phone && app.globalData.member.phone)
      {
        app.globalData.userset.phone=app.globalData.member.phone;        
      }
      if(!app.globalData.userset.name && app.globalData.member.memberinfo)
      {
        app.globalData.userset.name=app.globalData.member.memberinfo.name;        
      }
    }
 
    var updatepara ={};
    if(app.globalData.configdata.delivery_config)
    {
      try{
      var maxdistance=  app.globalData.configdata.delivery_config.intracity.maxdistance
       
         updatepara["maxdistance"] =maxdistance
          }
      catch(ex){

      }
    }
    updatepara["userset"]= app.globalData.userset     
    this.setData(updatepara)
    this.vliduserset();
    this.locationaddrfunc();
    },
  /**
   * 组件的方法列表
   */
  methods: {
    showselect:function(){
      var locationseachaddr= this.selectComponent("#locationseachaddr");
      if(locationseachaddr)
      {
        locationseachaddr.show();
      }
    },
    changelocation(ev){

      this.updatedata();
    
     },
 
    vliduserset:function(field){      
      var app= getApp(); 
      var updatemodel={}
      if(field)
      {
        updatemodel['vildfield.'+field]=app.globalData.userset[field]?true:false;
      }
      else{
        ["name","phone"].forEach(item=>{
          updatemodel['vildfield.'+item]=app.globalData.userset[item]?true:false;
        })
      }    
      this.setData(updatemodel)
    },
    inputname:function(e){
      var dataset=e.target.dataset       
      app.globalData.userset[dataset.field]= e.detail
      this.vliduserset(dataset.field)        
    },

    //获取手机号
    getPhoneNumber(e) {
      var that = this;
    
      var encryptedData = e.detail.encryptedData;
      if(!encryptedData){

        return ;
      }
      var iv = e.detail.iv;
      var code = e.detail.code;

      var app= getApp();     
      app.toolv2.getphone(encryptedData, iv,code).then(function (data) {             
        if (data["code"] == "success") {
          var upatemodel = {};
          upatemodel["userset.phone"] = data["obj"].phoneNumber;
          app.globalData.userset.phone= upatemodel["userset.phone"]         
          that.setData(upatemodel);
          that.vliduserset("phone")
        }            
      });
    },
    updatedata(){
     var that = this;
      var app = getApp();
      if (app.globalData.locationaddr&&app.globalData.locationaddr.status!=-1) {
       var distance= this.getdistance()


    
       var updatemodel={ locationaddr: app.globalData.locationaddr,locationaddrtxt: app.globalData.locationaddr.addrinfo,distance }
        this.setData(updatemodel)     
        that.triggerEvent("change",{status:1,obj:updatemodel})
      }
    },
    getdistance(){
      var app = getApp();
      if(app.globalData.locationaddr)
      {
        var locationaddr =app.globalData.locationaddr
        var intracity = app.globalData.configdata.delivery_config.intracity

       var distance =app.toolv2.distanceByLnglat(locationaddr.lng,locationaddr.lat,intracity.lng,intracity.lat)
        return distance    
      }

      return null
 

    },
    locationaddrfunc() {
      var app = getApp();
      var that =this
      if (app.globalData.locationaddr&&app.globalData.locationaddr.status!=-1) {
        that.updatedata()
        // this.setData({ locationaddr: app.globalData.locationaddr,locationaddrtxt: app.globalData.locationaddr.addrinfo })
      }
      else {        
        locationhelper.getcity().then(function (item) {
       
          if(item["status"]==-1){
           
            that.setData({ locationaddr: item,locationaddrtxt:item["message"] })
            /**失效的情况 */
            that.triggerEvent("change",{status:-1,message:item["message"]})
          }
          else
          {
            that.updatedata()
          //  that.setData({ locationaddr: item.locationaddr,locationaddrtxt: item.locationaddr.addrinfo})
          }  
        })
      }
    },


  }
})
